package cc.admin.modules.sys.controller;

import cc.admin.common.api.vo.Result;
import cc.admin.common.aspect.annotation.AutoLog;
import cc.admin.common.sys.base.controller.BaseController;
import cc.admin.common.sys.query.QueryGenerator;
import cc.admin.common.sys.vo.LoginUser;
import cc.admin.modules.sys.entity.SysPosition;
import cc.admin.modules.sys.service.ISysPositionService;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Description: 岗位管理
 * @Author: cc-admin
 * @Date: 2022-01-13
 * @Version: V1.0.0
 */
@Slf4j
@Api(tags = "岗位管理")
@RestController
@RequestMapping("/sys/position")
public class SysPositionController extends BaseController<SysPosition, ISysPositionService> {
    @Autowired
    private ISysPositionService sysPositionService;

    /**
     * 分页列表查询
     *
     * @param key
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "岗位管理-分页列表查询")
    @ApiOperation(value = "岗位管理-分页列表查询", notes = "岗位管理-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(
            SysPosition sysPosition,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            HttpServletRequest req) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        QueryWrapper<SysPosition> queryWrapper = QueryGenerator.initQueryWrapper(new SysPosition(), req.getParameterMap());
        if (sysPosition!=null && sysPosition.getPostName()!=null) {
            queryWrapper.like("post_name", sysPosition.getPostName());
        }
        queryWrapper.orderByAsc("px");
        Page<SysPosition> page = new Page<SysPosition>(pageNo, pageSize);
        IPage<SysPosition> pageList = sysPositionService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param sysPosition
     * @return
     */
    @AutoLog(value = "岗位管理-添加")
    @ApiOperation(value = "岗位管理-添加", notes = "岗位管理-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody SysPosition sysPosition) {
        LoginUser sysUser=(LoginUser)SecurityUtils.getSubject().getPrincipal();
        sysPositionService.save(sysPosition);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param sysPosition
     * @return
     */
    @AutoLog(value = "岗位管理-编辑")
    @ApiOperation(value = "岗位管理-编辑", notes = "岗位管理-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody SysPosition sysPosition) {
        sysPositionService.updateById(sysPosition);
        sysPositionService.updateUserPostName(sysPosition);
        return Result.ok("编辑成功!");
    }


    /**
     * 复制
     *
     * @param id
     * @param name
     * @return
     */
    @AutoLog(value = "岗位管理-复制")
    @ApiOperation(value = "岗位管理-复制", notes = "岗位管理-复制")
    @PostMapping(value = "/copy")
    public Result<?> copy(@RequestParam(name = "id", required = true) String id, @RequestParam(name = "name", required = true) String name) {
        SysPosition sysPosition = sysPositionService.getById(id);
        sysPosition.setId(IdUtil.fastUUID());
       // sysPosition.setName(name);
        sysPositionService.save(sysPosition);
        return Result.ok("复制成功！");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "岗位管理-通过id删除")
    @ApiOperation(value = "岗位管理-通过id删除", notes = "岗位管理-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        sysPositionService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "岗位管理-批量删除")
    @ApiOperation(value = "岗位管理-批量删除", notes = "岗位管理-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.sysPositionService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "岗位管理-通过id查询")
    @ApiOperation(value = "岗位管理-通过id查询", notes = "岗位管理-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        SysPosition sysPosition = sysPositionService.getById(id);
        return Result.ok(sysPosition);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param sysPosition
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SysPosition sysPosition) {
        return super.exportXls(request, sysPosition, SysPosition.class, "岗位管理");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SysPosition.class);
    }
    @GetMapping(value = "/positionAll")
    public Result<?> positionAll() {
        List<Map<String, Object>> dataList = sysPositionService.listMaps();
        return Result.ok(dataList);
    }

    /**
     * 岗位列表
     * @return
     */
    @PostMapping(value = "/postList")
    public Result<?> postList(@RequestParam(name = "type") String type) {
        List<Map<String, Object>> list = sysPositionService.postList(type);
        return Result.ok(list);
    }



}
